import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "@langchain/openai";
import "dotenv/config";
import { TextLoader } from "langchain/document_loaders/fs/text";
import {
  RecursiveCharacterTextSplitter,
  SupportedTextSplitterLanguages,
  TokenTextSplitter,
} from "langchain/text_splitter";
import { MemoryVectorStore } from "langchain/vectorstores/memory";

// console.log(SupportedTextSplitterLanguages);
const textLoader = new TextLoader("./data/kong.txt");
const text = await textLoader.load();
// console.log(text);

const splitter = new RecursiveCharacterTextSplitter({
  chunkSize: 100,
  chunkOverlap: 20,
});

const splitDocs = await splitter.splitDocuments(text);
// console.log(splitDocs[0]);

const embeddings = new OpenAIEmbeddings({
  configuration: {
    apiKey: process.env.OPENAI_API_KEY,
    baseURL: process.env.OPENAI_API_BASE,
  },
});
// const embeddingsResult = await embeddings.embedQuery(splitDocs[0].pageContent);
// console.log(embeddingsResult[0]);

const vectorstore = new MemoryVectorStore(embeddings);
await vectorstore.addDocuments(splitDocs);

// 检索 这里我们传入了参数 2，代表对应每个输入，我们想要返回相似度最高的两个文本内容
const retriever = vectorstore.asRetriever(2);
// 我们就可以使用 `retriever` 来进行文档的提取
const res = await retriever.invoke("茴香豆是做什么用的");
console.log(res);
